package com.ccoffers.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ccoffers.model.cardinfo.CreditCardCompany;
import com.ccoffers.model.cardinfo.CreditCardType;
import com.ccoffers.model.cardinfo.shopping.CreditCardOffer;
import com.ccoffers.model.shopping.Merchant;
import com.ccoffers.model.shopping.ShoppingCategory;
import com.ccoffers.service.impl.CRUDServiceImpl;
@Controller
@RequestMapping(value="/offerAdmin")
public class OfferDetailsAdminController {
	@Autowired
	private CRUDServiceImpl<CreditCardType> creditCardTypeService;
	
	@Autowired
	private CRUDServiceImpl<CreditCardCompany> creditCardCompanyService;
	
	@Autowired
	private CRUDServiceImpl<Merchant> merchantService;
	
	@Autowired
	private CRUDServiceImpl<CreditCardOffer> offerService;
	
	@Autowired
	private CRUDServiceImpl<ShoppingCategory> shoppingCategoryService;
	
	@RequestMapping(value="")
	public String displayOffers(Model model) {
		model.addAttribute("allMerchants", merchantService.loadAll(Merchant.class));
		model.addAttribute("allCardTypes", creditCardTypeService.loadAll(CreditCardType.class));
		model.addAttribute("allOfferingCompanies", creditCardCompanyService.loadAll(CreditCardCompany.class));
		model.addAttribute("allCategories", shoppingCategoryService.loadAll(ShoppingCategory.class));
		model.addAttribute("allOffers", offerService.loadAll(CreditCardOffer.class));
		return JspNames.OFFER_DETAILS_ADMIN;
		
	}
	
	@RequestMapping(value="/delete")
	public String deleteOffer(@RequestParam("offerId") Long id, Model model) {
		offerService.delete(offerService.retrieve(CreditCardOffer.class, id));
		return "redirect:/offerAdmin";
	}
	
	@RequestMapping(value="/add")
	public String addOffer(@RequestParam("offeringCompanyId") Long offeringCompanyId,
			@RequestParam("cardTypeId") Long cardTypeId,
			@RequestParam("shoppingCategoryId") Long shoppingCategoryId,
			@RequestParam("merchantNameId") Long merchantNameId,
			@RequestParam("offerText") String offerText,
			@RequestParam("tAndCURL") String tAndCURL,
			@RequestParam("validUpto") String validUpto, Model model) {
		CreditCardType cardType = creditCardTypeService.retrieve(CreditCardType.class, cardTypeId);
		CreditCardCompany company = creditCardCompanyService.retrieve(CreditCardCompany.class, offeringCompanyId);
		ShoppingCategory category = shoppingCategoryService.retrieve(ShoppingCategory.class, shoppingCategoryId);
		Merchant merchant = merchantService.retrieve(Merchant.class, merchantNameId);
		CreditCardOffer offer = new CreditCardOffer();
		offer.setCreditCardCompany(company);
		offer.setCreditCardType(cardType);
		offer.setMerchant(merchant);
		offer.setShoppingCategory(category);
		offer.setTermsAndConditionsURL(tAndCURL);
		offer.setOfferText(offerText);
		DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		try {
			Date date = format.parse(validUpto);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			offer.setValidUpto(calendar);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		offerService.create(offer);
		return "redirect:/offerAdmin";
	}
}
